// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        speedMin:5,
        speedMax:10,
        scaleMin:0.5,
        scaleMax:1,
        yMin:250,
        yMax:450,
        alphaMin:255,
        alphaMax:255
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.nw=this.node.width;
        this.nh=this.node.height;
        this.sd=Math.random()*(this.speedMax-this.speedMin)+this.speedMin;
        this.scaleFun();
        this.alphaFun();
        this.XYFun();
    },
    scaleFun:function(){
         if(this.scaleMax!=this.scaleMin){
            this.sf=Math.random()*(this.scaleMax-this.scaleMin)+this.scaleMin;
            this.node.width=Math.round(this.nw*this.sf);
            this.node.height=Math.round(this.nh*this.sf);
         }
    },
    alphaFun:function(){
        if(this.alphaMin!=this.alphaMax){
            this.node.opacity=Math.random()*(this.alphaMax-this.alphaMin)+this.alphaMin;
        }
   },
    XYFun:function(){
        this.node.y=Math.random()*(this.yMax-this.yMin)+this.yMin;
        this.node.x=cc.view.getVisibleSize().width+10;
    },
     update (dt) {
         if(this.node.x<cc.view.getVisibleOrigin().x-cc.view.getVisibleSize().width){
            this.sd=Math.random()*(this.speedMax-this.speedMin)+this.speedMin;
            this.scaleFun();
            this.XYFun();
            this.alphaFun();
         }else{
            this.node.x-=this.sd;
         }
     },
});
